Nuhman Paramban


@Nuhman Paramban

Express JS and FS package based NodeJS File Uploader

Express JS and FS will make NodeJS file uploading very easy. This code snippet will provide a single multipart file upload operation
Step 1 - Run npm install Express

Step 2 - Run npm install FS

Step 3 

Copy this code and run node filename.js



const app = require('express')();
var fs = require('fs');

app.post('/upload', function(req, res) {

var tmp_path = req.files.file.path;
var target_path = '/uploads/' + req.files.file.name;
fs.copyFile(tmp_path, target_path, function(err) {
if (err) throw err;
fs.unlink(tmp_path, function() {
if (err) throw err;
//res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
});
});


res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify({ file: req.files.file.name }));



}).listen(9222);